const app = getApp()
Page({
  data: {
      courseName:'',
      tokenID:'',
      tytpe:'',
      sysid:'',
      title:'',
      page: 1,                         
      moment: [],                   
      datamsg: false,
      is_modal_Hidden: true,
      is_modal_Msg: '请输入课程密码',
      passwordValue: '',
      courseLinkUrl: '',
      password: '',
      searchtype: false,
  },
  onLoad: function (options) {
      var _this = this;
      _this.setData({
          tokenID: app.data.tokenID,
          tytpe: options.tytpe,
          sysid: options.sysid,
          title: options.title
      })
      wx.setNavigationBarTitle({
          title: _this.data.title
      })
      _this.onPullDownRefresh();
  },
  onPullDownRefresh: function () {
      var _this = this;
      _this.setData({
          page: 1
      })
      wx.showNavigationBarLoading();
      _this.gtemomentList(_this.data.courseName)
  },
    gtemomentList: function (name) {
      var _this = this;
      wx.request({
          url: app.data.api + 'group/getGroupCourses',
          data: {
              "name":name,
              "token": _this.data.tokenID,
              "gid": _this.data.sysid
          },
          method: "POST",
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          success: function (res) {
              if (res.data.status) {
                  var moment_list = [];
                  var json = {}
                  for (var i = 0; i < res.data.data.length; i++) {
                      json = res.data.data[i]
                      for (var j = 0; j < res.data.data[i].groupCourses.length ; j ++){
                          if(j == 0){
                              res.data.data[i].groupCourses[j].clique = true
                          }else{
                              res.data.data[i].groupCourses[j].clique = false
                          }
                          res.data.data[i].groupCourses[j].index = i
                      }
                      moment_list.push(json);
                      
                  }
                  _this.setData({
                      page: _this.data.page + 1,
                      moment: moment_list
                  })
                  wx.stopPullDownRefresh();
                  wx.hideLoading();
                  wx.hideNavigationBarLoading();
              } else {
                  wx.hideLoading();
                  wx.showModal({
                      title: '提示',
                      content: res.data.msg,
                      success: function (res) {
                          if (res.confirm) {
                              wx.redirectTo({
                                  url: '../login/login'
                              })
                          } else if (res.cancel) {
                              wx.redirectTo({
                                  url: '../login/login'
                              })
                          }
                      }
                  });
              }
          }
      })
  },
    onMyEvent: function (event) {
        var _this = this;
        this.setData({
            passwordValue: event.detail.passwordValue,
        })
        if (_this.data.passwordValue == _this.data.password) {
            wx.navigateTo({
                url: _this.data.courseLinkUrl
            })
            _this.setData({
                is_modal_Hidden: true,
                password: '',
                courseLinkUrl: ''
            })
        } else {
            wx.showToast({
                title: '课程密码有误！请重新输入。',
                icon: 'none',
                duration: 800
            })
        }
    },
    courseLink(event) {
        var _this = this;
        var url = event.currentTarget.dataset.url;
        var needpwd = event.currentTarget.dataset.needpwd;
        var password = event.currentTarget.dataset.password;
        if (needpwd == 1000) {
            wx.navigateTo({
                url: url
            })
        } else {
            _this.setData({
                is_modal_Hidden: false,
                password: password,
                courseLinkUrl: url
            })
        }
    },
    show_info:function(event){
        var _this = this;
        var key = event.currentTarget.dataset.key;
        var index = event.currentTarget.dataset.index;
        var cliqueType = event.currentTarget.dataset.cliquetype;
        var clique = 'moment[' + key + '].groupCourses[' + index+'].clique';
        _this.setData({
            [clique]: !cliqueType
        })
    },
    changeText: function (event) {
        var _this = this;
        console.log(event.detail.value.length > 0)
        if (event.detail.value.length > 0) {
            _this.setData({
                courseName: event.detail.value,
                searchtype: true,
            })
        } else {
            _this.setData({
                searchtype: false,
                courseName: ''
            })
        }
    },
    emptysearch: function () {
        var _this = this;
        _this.setData({
            courseName: '',
            searchtype: false
        })
        setTimeout(function () {
            _this.gtemomentList(_this.data.courseName)
        }, 500)
    },
    searchCourse: function () {
        var _this = this;
        if (_this.data.courseName) {
            _this.gtemomentList(_this.data.courseName)
        } else {
            wx.showModal({
                title: '提示',
                content: '请输入搜索关键词',
                showCancel: false,
                confirmColor: '#04A9F4',
                success: function (res) {
                    if (res.confirm) {
                    } else if (res.cancel) {
                    }
                }
            })
            return false;
        }
    },
    showSearch:function(){
        this.emptysearch()
    }
})